<?php
class Net {
	public function getIp($sIp = null, $sReturnType = 'string', $bCheck = false) {
		if ($sIp) {
			if (preg_match ( '/^\d+$/', $sIp )) {
				$sIpString = long2ip ( $sIp );
				if ($bCheck) {
					return $sIpString;
				} else {
					if ($sIp == self::getIp ( $sIpString, 'long', ture )) {
						if ($sReturnType == 'string') {
							return $sIpString;
						} elseif ($sReturnType == 'long') {
							return $sIp;
						}
					} else {
						return false;
					}
				}
			} elseif (preg_match ( '/^\d+\.\d+\.\d+\.\d+$/', $sIp )) {
				$iIpLong = sprintf ( '%u', ip2long ( $sIp ) );
				if ($bCheck) {
					return $iIpLong;
				} else {
					if ($sIp == self::getIp ( $iIpLong, 'string', ture )) {
						if ($sReturnType == 'string') {
							return $sIp;
						} elseif ($sReturnType == 'long') {
							return $iIpLong;
						}
					} else {
						return false;
					}
				}
			} else {
				return false;
			}
		} else {
			if ($_SERVER ['HTTP_CLIENT_IP']) {
				$sIp = $_SERVER ['HTTP_CLIENT_IP'];
			} elseif ($_SERVER ['HTTP_X_FORWARDED_FOR']) {
				$sIp = $_SERVER ['HTTP_X_FORWARDED_FOR'];
			} else {
				$sIp = $_SERVER ['REMOTE_ADDR'];
			}
			return self::getIp ( $sIp, $sReturnType );
		}
		
		return false;
	}
	
	public function parseAgent() {
		$aMatch = array ();
		if (preg_match ( '/msie\s+(.+?);/i', $_SERVER ['HTTP_USER_AGENT'], $aMatch )) {
			$sBrowse = 'IE';
			$sBrowseVer = $aMatch [1];
		} elseif (preg_match ( '/Firefox\/(.+?)\s/i', $_SERVER ['HTTP_USER_AGENT'], $aMatch )) {
			$sBrowse = 'Firefox';
			$sBrowseVer = $aMatch [1];
		} elseif (preg_match ( '/Chrome\/(.+?)\s/i', $_SERVER ['HTTP_USER_AGENT'], $aMatch )) {
			$sBrowse = 'Chrome';
			$sBrowseVer = $aMatch [1];
		} elseif (preg_match ( '/Opera\/(.+?)\s/i', $_SERVER ['HTTP_USER_AGENT'], $aMatch )) {
			$sBrowse = 'Opera';
			$sBrowseVer = $aMatch [1];
		} elseif (preg_match ( '/Safari\/(.+?)\s/i', $_SERVER ['HTTP_USER_AGENT'], $aMatch )) {
			$sBrowse = 'Safari';
			$sBrowseVer = $aMatch [1];
		} elseif (preg_match ( '/Safari\/(.+?)\s/i', $_SERVER ['HTTP_USER_AGENT'], $aMatch )) {
			$sBrowse = 'Safari';
			$sBrowseVer = $aMatch [1];
		}
		if (preg_match ( '/Windows NT (.*?);/i', $_SERVER ['HTTP_USER_AGENT'], $aMatch )) {
			$sOs = 'Windows';
			switch ($aMatch [1]) {
				case '5.0' :
					$sOsVer = '2000';
					break;
				case '5.1' :
					$sOsVer = 'XP';
					break;
				case '5.2' :
					$sOsVer = 'SERVER 2003';
					break;
				case '6.0' :
					$sOsVer = 'Vista';
					break;
				case '6.1' :
					$sOsVer = '7';
					break;
			}
		} elseif (preg_match ( '/Windows (\d+);/', $_SERVER ['HTTP_USER_AGENT'], $aMatch )) {
			$sOs = 'Windows';
			$sOsVer = $aMatch [1];
		} elseif (preg_match ( '/FreeBSD/', $_SERVER ['HTTP_USER_AGENT'] )) {
			$sOs = 'FreeBSD';
		} elseif (preg_match ( '/Linux/', $_SERVER ['HTTP_USER_AGENT'] )) {
			$sOs = 'Linux';
		} elseif (preg_match ( '/Mac OS/', $_SERVER ['HTTP_USER_AGENT'] )) {
			$sOs = 'Mac OS';
		}
		
		return array ('browse' => $sBrowse, 'browsever' => $sBrowseVer, 'os' => $sOs, 'osver' => $sOsVer );
	}
	
	public function getOs() {
	
	}
	
	/**
	 * 发送电子邮件
	 * 同时发送给多个人时$sTo用数组表示
	 * 同时抄送给多个人时$sCc用数组表示
	 * 当需要smtp验证时$sFrom用数组表示 name:用户名,password:密码,host:smtp服务器地址,port:端口,mail:验证的email地址,from:发送的email地址没有时使用mail
	 *
	 * @param string $sTo
	 * @param string $sSubject
	 * @param string $sMessage
	 * @param string $sFrom
	 * @param string $sCc
	 * @return bool
	 */
	public function sendMail($sTo, $sSubject, $sMessage, $sFrom, $sCc = null) {
		$aTo = self::_parseMailAddress ( $sTo );
		$sHeader = '';
		if (is_array ( $aTo ['name'] )) {
			$sTo = implode ( ',', $aTo ['mail'] );
			$sHeader .= 'To: ' . implode ( ', ', $aTo ['full'] ) . "\n";
		} else {
			$sTo = $aTo ['mail'];
			$sHeader .= 'To: ' . $aTo ['full'] . "\n";
		}
		if (is_array ( $sFrom )) {
			if ($sFrom ['from']) {
				$aFrom = self::_parseMailAddress ( $sFrom ['from'] );
			} else {
				$aFrom = self::_parseMailAddress ( $sFrom ['mail'] );
			}
		} else {
			$aFrom = self::_parseMailAddress ( $sFrom );
		}
		$sHeader .= 'From: ' . $aFrom ['full'] . "\n";
		if ($sCc) {
			$aCc = self::_parseMailAddress ( $sCc );
			$sHeader .= 'Cc: ' . implode ( ', ', $aCc ['full'] ) . "\n";
		}
		
		if (is_array ( $sFrom )) {
			$sBoundary = md5 ( uniqid () );
			$rSocket = Base::$oBase->Socket_create ();
			if (! $rSocket) {
				return false;
			}
			
			$iPort = $sFrom ['port'] ? $sFrom ['port'] : 25;
			$rConn = Base::$oBase->Socket_connect ( $sFrom ['host'], $iPort );
			if (! $rConn) {
				return false;
			}
			
			$sData = 'Return-Path:' . "\n";
			$sData .= 'From: ' . $sFrom ['mail'] . "\n";
			$sData .= 'To: ' . $sTo . "\n";
			$sData .= 'Message-id: <' . $sFrom ['mail'] . '>' . "\n";
			$sData .= 'Content-type: multipart/mixed; boundary="' . $sBoundary . '"' . "\n";
			$sData .= 'subject:' . $sSubject . "\n";
			$sData .= "\n" . '--' . $sBoundary . "\n";
			$sData .= 'Content-type: text/plain; charset=utf-8' . "\n";
			$sData .= 'Content-transfer-encoding: base64' . "\n\n";
			$sData .= chunk_split ( base64_encode ( $sMessage ) ) . "\n";
			/*$sData .= "\n".'--'.$sBoundary. "\n";
			$sData .= 'Content-type: text/html; name=attachment.html'."\n";
			$sData .= 'Content-disposition: inline; filename=attachment.html'."\n";
			$sData .= 'Content-transfer-encoding: base64'."\n\n";
			$sMessage = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>'.$sSubject.'</title></head><body>'.$sMessage.'</body></html>';
			$sData .= chunk_split(base64_encode($sMessage))."\n";*/
			$sData .= '--' . $sBoundary . "--\n";
			echo nl2br ( $sData );
			if (( int ) Base::$oBase->Socket_send ( "ehlo helo\n" ) > 399) {
				return false;
			}
			if (( int ) Base::$oBase->Socket_send ( "auth login\n" ) > 399) {
				return false;
			}
			if (( int ) Base::$oBase->Socket_send ( base64_encode ( $sFrom ['name'] ) . "\n" ) > 399) {
				return false;
			}
			if (( int ) Base::$oBase->Socket_send ( base64_encode ( $sFrom ['password'] ) . "\n" ) > 399) {
				return false;
			}
			if (( int ) Base::$oBase->Socket_send ( 'mail from:' . $sFrom ['mail'] . "\n" ) > 399) {
				return false;
			}
			if (( int ) Base::$oBase->Socket_send ( "rcpt to:" . $sTo . "\n" ) > 399) {
				return false;
			}
			if (( int ) Base::$oBase->Socket_send ( "data\n" ) > 399) {
				return false;
			}
			if (( int ) Base::$oBase->Socket_send ( $sData . "\n.\n" ) > 399) {
				return false;
			}
			if (( int ) Base::$oBase->Socket_send ( "quit\n" ) > 399) {
				return false;
			}
			return true;
		} else {
			$sHeader = 'MIME-Version: 1.0' . "\n" . $sHeader;
			$sHeader .= 'Content-type: text/html; charset=utf-8' . "\n";
			return @mail ( $sTo, '=?UTF-8?B?' . base64_encode ( $sSubject ) . '?=', $sMessage, $sHeader );
		}
	}
	
	private function _parseMailAddress($sMailAddress) {
		$aResult = array ();
		if (is_array ( $sMailAddress )) {
			foreach ( $sMailAddress as $sRow ) {
				$aData = self::_parseMailAddress ( $sRow );
				$aResult ['name'] [] = $aData ['name'];
				$aResult ['mail'] [] = $aData ['mail'];
				$aResult ['full'] [] = $aData ['full'];
			}
		} else {
			$aMatch = array ();
			preg_match ( '/^(.+)\s*<(.+)>$/', $sMailAddress, $aMatch );
			if (count ( $aMatch )) {
				$aResult ['name'] = $aMatch [1];
				$aResult ['mail'] = $aMatch [2];
				$aResult ['full'] = $aMatch [1] . ' <' . $aMatch [2] . '>';
			} else {
				preg_match ( '/^(.+)@(.+)$/', $sMailAddress, $aMatch );
				if (count ( $aMatch )) {
					$aResult ['name'] = $aMatch [1];
					$aResult ['mail'] = $aMatch [1] . '@' . $aMatch [2];
					$aResult ['full'] = $aResult ['name'] . ' <' . $aResult ['mail'] . '>';
				} else {
					$aResult = false;
				}
			}
		}
		return $aResult;
	}
}
?>